const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const UglifyJSPlugin = require('uglifyjs-webpack-plugin')
const chalk = require('chalk');

// Create multiple instances
const extractCSS = new ExtractTextPlugin('[name]-one.css');
const extractLESS = new ExtractTextPlugin('[name]-two.css');

// const css_loader = "css-loader?modules&localIdentName=[path][name]---[local]---[hash:base64:5]"
const css_loader = "css-loader?modules&minimize&localIdentName=[path][name]---[local]";//https://github.com/webpack-contrib/css-loader#minimize

let buildRunning = false;
const webpackConfig = {
    entry: {
        vendor: ["react"],
        app: path.resolve(__dirname, './src/index.js')
    },
    // entry: path.resolve(__dirname, './src/index.js'),//这里也可以直接写index.1.less，但是也会生成一个js文件
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'index.js'
    },
    module: {
        // webpack3.x不用loaders，用rules
        // loaders: [
        //     {
        //         test: /\.json$/,
        //         loader: 'json-loader',
        //     },
        //     {
        //         test: /\.less$/,
        //         loaders: ['style-loader', css_loader, 'less-loader'],//1.x的版本是不需要加 "-loader"的，现在用的是3.x版本，需要加
        //     },
        //     {
        //         test: /\.jsx?$/,
        //         loader: 'babel-loader',
        //         exclude: [/node_modules/, './www/comm/public'],
        //     }
        // ],

        rules: [
            {
                test: /\.less$/i,
                use: extractCSS.extract([css_loader, 'less-loader'])
            },
            //没有这里的配置，只有上面的loader，会报错的，这个跟webpack1.x的配置方法不一样
            {
                test: /\.jsx?$/,
                exclude: /(node_modules|bower_components)/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['react']
                    }
                }
            }
        ],
    },
    devtool: "#source-map",

    plugins: [
        extractCSS, extractLESS,
        new webpack.optimize.CommonsChunkPlugin({
            name: "vendor",
            filename: "vendor.js",
            // (给 chunk 一个不同的名字)

            minChunks: Infinity,
            // (随着 entry chunk 越来越多，
            // 这个配置保证没其它的模块会打包进 vendor chunk)
        }),
        // new UglifyJSPlugin({//https://doc.webpack-china.org/plugins/uglifyjs-webpack-plugin/
        //     sourceMap: true,//只有设置了devtool才会生效
        //     uglifyOptions: {
        //         sourceMap: true,
        //         ie8: false,
        //         ecma: 8,
        //         parse: {},
        //         // mangle:false,//不混淆名字
        //         mangle: {
        //             keep_fnames: true,
        //             keep_classnames: true,
        //             properties: {//https://github.com/mishoo/UglifyJS2/tree/harmony#mangle-properties-options
        //                 // mangle property options
        //                 debug: true
        //             }
        //         },
        //         output: {
        //             comments: false,
        //             beautify: true,
        //             keep_quoted_props: true,
        //         },
        //         compress: {//https://github.com/mishoo/UglifyJS2/tree/harmony#compress-options
        //             unused: true,
        //             dead_code: true,
        //             warnings: false,
        //             pure_funcs: ['console.log'],
        //             drop_console: true,
        //             drop_debugger: true,
        //             join_vars: false,
        //             collapse_vars: false,
        //         },
        //         warnings: false
        //     }

        // }),

        new webpack.ProgressPlugin((percentage, msg) => {
            const stream = process.stderr;
            const stream2 = process.stdout;

            if (!buildRunning) {
                buildRunning = true;
                startTime = new Date();
            } else if (stream.isTTY && percentage < 0.71) {
                stream.cursorTo(0);
                stream.write(`📦  ${chalk.magenta(msg)} ${percentage}`);
                stream.clearLine(1);
            } else if (percentage === 1) {
                const now = new Date();
                const buildTime = `${(now - startTime) / 1000}s`;
                console.log(chalk.green(`\nwebpack: bundle build completed in ${buildTime}.`));

                buildRunning = false;
            }





            // if (!buildRunning) {
            //     buildRunning = true;
            //     startTime = new Date();
            // } else if (stream2.isTTY && percentage < 0.71) {
            //     stream2.cursorTo(0);
            //     stream2.write(`📦  ${chalk.magenta(msg)}`);
            //     stream2.clearLine(1);
            // } else if (percentage === 1) {
            //     const now = new Date();
            //     const buildTime = `${(now - startTime) / 1000}s`;
            //     console.log(chalk.green(`\nwebpack: bundle build completed in ${buildTime}.`));

            //     buildRunning = false;
            // }
        }),
    ],
    stats: "verbose",
}


module.exports = webpackConfig;
